package io.renren.modules.generator.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.Query;
import io.renren.modules.generator.dao.EnterpriseMainDao;
import io.renren.modules.generator.entity.EnterpriseMainEntity;
import io.renren.modules.generator.service.EnterpriseMainService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.List;
import java.util.Map;
import java.util.Objects;


@Service("enterpriseMainService")
public class EnterpriseMainServiceImpl extends ServiceImpl<EnterpriseMainDao, EnterpriseMainEntity> implements EnterpriseMainService {

    @Autowired
    private EnterpriseMainDao enterpriseMainDao;

    @Override
    public PageUtils queryPage(Map<String, Object> params,EnterpriseMainEntity enterpriseMainEntity) {
        QueryWrapper<EnterpriseMainEntity> wrapper = new QueryWrapper<>();
        if(enterpriseMainEntity != null && Objects.nonNull(enterpriseMainEntity.getName())){
            wrapper.like("name",enterpriseMainEntity.getName());
        }
        if (enterpriseMainEntity != null && StringUtils.isNotBlank(enterpriseMainEntity.getDeptId())) {
            List<String> list = enterpriseMainDao.getList(enterpriseMainEntity.getDeptId());
            if (!CollectionUtils.isEmpty(list)) {
                list.add(enterpriseMainEntity.getDeptId());
                wrapper.in("dept_id", list);
            } else {
                wrapper.in("dept_id", enterpriseMainEntity.getDeptId());
            }
        }
        wrapper.orderByAsc("id");
        IPage<EnterpriseMainEntity> page = this.page(
                new Query<EnterpriseMainEntity>().getPage(params),
                wrapper
        );

        return new PageUtils(page);
    }

    @Override
    public PageUtils queryEvaluateListPage(Map<String, Object> params, EnterpriseMainEntity enterpriseMainEntity) {
        QueryWrapper<EnterpriseMainEntity> wrapper = new QueryWrapper<>();
        if(Objects.nonNull(enterpriseMainEntity.getName())){
            wrapper.like("name",enterpriseMainEntity.getName());
        }
        wrapper.inSql("id"," select enterprise_id from enterprise_evaluate ");
        wrapper.orderByAsc("id");
        IPage<EnterpriseMainEntity> page = this.page(
                new Query<EnterpriseMainEntity>().getPage(params),
                wrapper
        );

        return new PageUtils(page);
    }

    @Override
    public EnterpriseMainEntity getByDeptid(Integer deptid) {
        QueryWrapper<EnterpriseMainEntity> wrapper = new QueryWrapper<>();
        wrapper.lambda().eq(EnterpriseMainEntity::getDeptId,deptid);
        return enterpriseMainDao.selectOne(wrapper);
    }

}
